package org.zbclan.bf2.rcon.client.statistics;

import java.util.HashMap;
import java.util.Map;

import org.zbclan.bf2.rcon.client.BF2RconConstants;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.rednels.ofcgwt.client.ChartWidget;
import com.rednels.ofcgwt.client.model.ChartData;
import com.rednels.ofcgwt.client.model.elements.PieChart;
import com.romeo_foxtrot.labs.solr.client.Solr;
import com.romeo_foxtrot.labs.solr.client.json.Hits;
import com.romeo_foxtrot.labs.solr.client.json.Luke;
import com.romeo_foxtrot.labs.solr.client.json.SearchResult;
import com.romeo_foxtrot.labs.solr.client.json.SearchResult.FacetCounts.FacetField;

public class Statistics extends Composite {

	private static StatisticsUiBinder uiBinder = GWT
			.create(StatisticsUiBinder.class);

	interface StatisticsUiBinder extends UiBinder<Widget, Statistics> {
	}

	@UiField
	HasWidgets container;

	public Statistics() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	public void show() {

		/*
		 * TODO
		 */
		// /*
		// * http://bf2.zbclan.org/bf2rcon/
		// */
		// Window.alert(GWT.getHostPageBaseURL());
		// /*
		// * http://bf2.zbclan.org/bf2rcon/VAADIN/widgetsets/bf2rcon/
		// */
		// Window.alert(GWT.getModuleBaseURL());

		String moduleBaseURL = GWT.getModuleBaseURL();
		String baseUrl = moduleBaseURL.substring(0,
				moduleBaseURL.indexOf("/VAADIN/widgetsets/bf2rcon"));
		Solr solr = new Solr(baseUrl + "/solr", false) {

			@Override
			protected void searchCallback(SearchResult data) {
			}

			@Override
			protected void lukeCallback(Luke data) {

				StringBuilder facetBuilder = new StringBuilder();

				JsArray<Luke.Field> fields = data.getFields();
				for (int i = 0; i < fields.length(); i++) {
					Luke.Field f = fields.get(i);
					String name = f.getName();
					facetBuilder.append("&facet.field=" + name + "&f." + name
							+ ".facet.limit=" + f.getDistinct());
				}

				facetSearchAsync(facetBuilder.toString(), "*:*", "");

				return;
			}

			@Override
			protected void facetSearchCallback(SearchResult data) {

				Map<String, JsArray<Hits>> map = new HashMap<String, JsArray<Hits>>();
				JsArray<FacetField> fields = data.getFacetCounts()
						.getFacetFields();
				for (int i = 0; i < fields.length(); i++) {
					FacetField f = fields.get(i);
					map.put(f.getName(), f.getTerms());
				}

				JsArray<Hits> provider = map.get("audio.provider");

				ChartWidget chart = new ChartWidget();
				chart.setTitle("Audio Renderer");

				ChartData cd = new ChartData();

				PieChart pieChart = new PieChart();

				for (int i = 0; i < provider.length(); i++) {
					Hits hits = provider.get(i);
					String name = hits.getName();
					// Window.alert((name == null) + "");
					if (name == null) {
						name = BF2RconConstants.INSTANCE.unknown();
					}
					pieChart.addSlice(hits.getValue(), name);
				}

				cd.addElements(pieChart);
				chart.setChartData(cd);
				chart.setSize("400px", "300px");

				container.add(chart);

				return;
			}
		};
		solr.lukeAsync();

		return;
	}
}
